<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试完整 Cadillac CT6 Object DBC</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .error { color: red; font-weight: bold; }
        .success { color: green; font-weight: bold; }
        .warning { color: orange; }
        .stats { display: flex; gap: 20px; margin: 10px 0; flex-wrap: wrap; }
        .stat-item { background: #f0f0f0; padding: 10px; border-radius: 5px; min-width: 150px; }
        .progress { background: #f0f0f0; border-radius: 10px; overflow: hidden; margin: 10px 0; }
        .progress-bar { background: #4CAF50; height: 20px; transition: width 0.3s; }
        .log { background: #f9f9f9; padding: 10px; margin: 10px 0; border-left: 4px solid #ccc; max-height: 300px; overflow-y: auto; }
    </style>
</head>
<body>
    <h1>测试完整 Cadillac CT6 Object DBC 解析</h1>
    
    <div id="progress-section" style="display: none;">
        <div class="progress">
            <div class="progress-bar" id="progress-bar" style="width: 0%"></div>
        </div>
        <div id="progress-text">正在加载文件...</div>
    </div>
    
    <button onclick="startTest()" id="test-btn">开始测试</button>
    
    <div id="result"></div>

    <script src="dbc-parser.js"></script>
    <script>
        function updateProgress(percent, text) {
            document.getElementById('progress-bar').style.width = percent + '%';
            document.getElementById('progress-text').textContent = text;
        }
        
        async function startTest() {
            const resultDiv = document.getElementById('result');
            const progressSection = document.getElementById('progress-section');
            const testBtn = document.getElementById('test-btn');
            
            // 显示进度条
            progressSection.style.display = 'block';
            testBtn.disabled = true;
            
            try {
                updateProgress(10, '正在加载 cadillac_ct6_object.dbc 文件...');
                
                const response = await fetch('../opendbc/opendbc/dbc/cadillac_ct6_object.dbc');
                if (!response.ok) {
                    throw new Error(`HTTP错误: ${response.status}`);
                }
                
                updateProgress(30, '文件加载完成，正在读取内容...');
                const content = await response.text();
                
                updateProgress(50, '开始解析 DBC 文件...');
                const startTime = performance.now();
                
                // 解析文件
                const database = parseDBCContent(content, 'cadillac_ct6_object.dbc');
                
                const endTime = performance.now();
                const parseTime = (endTime - startTime).toFixed(2);
                
                updateProgress(100, '解析完成！');
                
                // 显示结果
                resultDiv.innerHTML = `
                    <div class="success">✓ 解析成功！</div>
                    
                    <div class="stats">
                        <div class="stat-item">
                            <strong>解析时间:</strong><br>${parseTime} ms
                        </div>
                        <div class="stat-item">
                            <strong>文件大小:</strong><br>${(content.length / 1024).toFixed(1)} KB
                        </div>
                        <div class="stat-item">
                            <strong>帧数量:</strong><br>${database.frames.length}
                        </div>
                        <div class="stat-item">
                            <strong>总信号数:</strong><br>${database.frames.reduce((sum, frame) => sum + frame.signals.length, 0)}
                        </div>
                        <div class="stat-item">
                            <strong>节点数量:</strong><br>${database.nodes.length}
                        </div>
                        <div class="stat-item">
                            <strong>警告数量:</strong><br>${database.warnings.length}
                        </div>
                    </div>
                    
                    ${database.warnings.length > 0 ? `
                        <h3>警告信息（前20条）:</h3>
                        <div class="log">
                            ${database.warnings.slice(0, 20).map((w, i) => 
                                `<div>${i+1}. 行 ${w.line}: ${w.message}</div>`
                            ).join('')}
                            ${database.warnings.length > 20 ? `<div><strong>... 还有 ${database.warnings.length - 20} 条警告</strong></div>` : ''}
                        </div>
                    ` : ''}
                    
                    <h3>示例帧信息（前10帧）:</h3>
                    <div class="log">
                        ${database.frames.slice(0, 10).map((frame, i) => `
                            <div style="margin: 5px 0; padding: 8px; background: ${i % 2 === 0 ? '#f5f5f5' : '#ffffff'}; border-radius: 4px;">
                                <strong>${frame.name}</strong> (ID: 0x${frame.canId.toString(16).toUpperCase().padStart(3, '0')}, DLC: ${frame.dlc})
                                <br><small>信号数量: ${frame.signals.length}${frame.comment ? ` | 注释: ${frame.comment.substring(0, 50)}${frame.comment.length > 50 ? '...' : ''}` : ''}</small>
                            </div>
                        `).join('')}
                        ${database.frames.length > 10 ? `<div><strong>... 还有 ${database.frames.length - 10} 帧</strong></div>` : ''}
                    </div>
                    
                    <h3>节点列表（前20个）:</h3>
                    <div class="log">
                        ${database.nodes.slice(0, 20).join(', ')}
                        ${database.nodes.length > 20 ? ` ... 还有 ${database.nodes.length - 20} 个节点` : ''}
                    </div>
                `;
                
                // 隐藏进度条
                setTimeout(() => {
                    progressSection.style.display = 'none';
                    testBtn.disabled = false;
                }, 1000);
                
            } catch (error) {
                console.error('测试失败:', error);
                resultDiv.innerHTML = `<div class="error">✗ 解析失败: ${error.message}</div>`;
                progressSection.style.display = 'none';
                testBtn.disabled = false;
            }
        }
    </script>
</body>
</html> 